using RRX.DragDrop;
using RRX.DragDrop.Utilities;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.Security.Cryptography;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Xml;
using System.Xml.Linq;

namespace RRX.Xml.ProjectTree
{
    /// <summary>
    /// Interaktionslogik für PageProjectTree.xaml
    /// </summary>
    public partial class PageProjectTree : Page , IDropTarget
    {
        public PageProjectTree()
        {
            InitializeComponent();

            DataContext = this;

            Templates_Generate();
            Xml_Initialize();

            Templates.ItemsSource = null;
            Templates.ItemsSource = aTemplateDefault.Children;


            Xml.ItemsSource = null;
            Xml.ItemsSource = XmlDefault.Children;


         
        }


        // ---------- Templates ----------

        ItemProjectTreeDefault aTemplateDefault = new ItemProjectTreeDefault();

        private void Templates_Generate()
        {
            aTemplateDefault = new ItemProjectTreeDefault();

            ItemProjectTreeProject templateProject = new ItemProjectTreeProject();
            templateProject.Name = "Project-Template";
            aTemplateDefault.Children.Add(templateProject);

            ItemProjectTreeAsp templateAsp = new ItemProjectTreeAsp();
            templateAsp.Name = "Asp-Template";
            aTemplateDefault.Children.Add(templateAsp);


            ItemProjectTreeSystem templateSystem = new ItemProjectTreeSystem();
            templateSystem.Name = "System-Template";
            aTemplateDefault.Children.Add(templateSystem);

            ItemProjectTreePage templatePage = new ItemProjectTreePage();
            templatePage.Name = "Page-Template";
            aTemplateDefault.Children.Add(templatePage);

            ItemProjectTreePageHeizkreis templatePageHeizkreis = new ItemProjectTreePageHeizkreis();
            templatePageHeizkreis.Name = "Page-Heizkreis";
            aTemplateDefault.Children.Add(templatePageHeizkreis);

            ItemProjectTreePageLüftung templatePageLüftung = new ItemProjectTreePageLüftung();
            templatePageLüftung.Name = "Page-Lüftung";
            aTemplateDefault.Children.Add(templatePageLüftung);

            ItemProjectTreeLine templateLine = new ItemProjectTreeLine();
            templateLine.Name = "Line-Template";
            aTemplateDefault.Children.Add(templateLine);

            ItemProjectTreeLoopSystem templateLoopSystem = new ItemProjectTreeLoopSystem();
            templateLoopSystem.Name = "LoopSystem-Template";
            aTemplateDefault.Children.Add(templateLoopSystem);

            ItemProjectTreeLoopPage templateLoopPage = new ItemProjectTreeLoopPage();
            templateLoopPage.Name = "LoopPage-Template";
            aTemplateDefault.Children.Add(templateLoopPage);

            ItemProjectTreeLoopPumpe templateLoopPumpe = new ItemProjectTreeLoopPumpe();
            templateLoopPumpe.Name = "LoopPumpe-Template";
            aTemplateDefault.Children.Add(templateLoopPumpe);

            ItemProjectTreeLoopFühler templateLoopFühler = new ItemProjectTreeLoopFühler();
            templateLoopFühler.Name = "LoopFühler-Template";
            aTemplateDefault.Children.Add(templateLoopFühler);

            ItemProjectTreeLoopVentil templateLoopVentil = new ItemProjectTreeLoopVentil();
            templateLoopVentil.Name = "LoopVentil-Template";
            aTemplateDefault.Children.Add(templateLoopVentil);
        }





        // ---------- Xml ----------

        ItemProjectTreeDefault XmlDefault = new ItemProjectTreeDefault();

        private void Xml_Initialize()
        {
            XmlDefault = new ItemProjectTreeDefault();

            ItemProjectTreeXml templateXml = new ItemProjectTreeXml();
            XmlDefault.Children.Add(templateXml);
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            XElement element = new XElement("xml");

            foreach (IItemProjectTree item in XmlDefault.Children[0].Children)
            {
                element.Add(item.Xml());
            }

            TextBox.Text = element.ToString(); 
        }


        // ---------- Tree ----------
        private void Tree_Loaded(object sender, RoutedEventArgs e)
        {
            // Prevent automatic horizontal and vertical scrolling by subscribing to ScrollContentPresenter's RequestBringIntoView event and flag it as handled.
            var p = (sender as TreeView).GetVisualDescendent<ScrollContentPresenter>();
            if (p != null)
            {
                p.RequestBringIntoView -= this.Tree_RequestBringIntoView;
                p.RequestBringIntoView += this.Tree_RequestBringIntoView;
            }
        }
        private void Tree_RequestBringIntoView(object sender, RequestBringIntoViewEventArgs e)
        {
            e.Handled = true;
        }


        // ---------- DragDrop ----------

        void IDropTarget.DragOver(IDropInfo dropInfo)
        {
            try
            {
                IItemProjectTree sourceItem = dropInfo.Data as IItemProjectTree;
                IItemProjectTree targetItem = dropInfo.TargetItem as IItemProjectTree;

                if (sourceItem != null)
                {
                    if (sourceItem == null || targetItem == null)
                    {
                        dropInfo.Effects = DragDropEffects.None;
                        return;
                    }

                    if (IsDropTargetChild(sourceItem, targetItem) || targetItem == sourceItem)
                    {
                        dropInfo.Effects = DragDropEffects.None;
                        return;
                    }



                    if (dropInfo.InsertPosition.ToString() == "BeforeTargetItem" || dropInfo.InsertPosition.ToString() == "AfterTargetItem")
                    {
                        dropInfo.Effects = DragDropEffects.Copy;
                        dropInfo.DropTargetAdorner = DropTargetAdorners.Insert;
                    }
                    else if (dropInfo.InsertPosition.ToString().Contains("TargetItemCenter"))
                    {
                        if (IsAllowedChild(targetItem, sourceItem))
                        {
                            dropInfo.Effects = DragDropEffects.Copy;
                            dropInfo.DropTargetAdorner = DropTargetAdorners.Highlight;
                        }
                        else
                        {
                            dropInfo.Effects = DragDropEffects.None;
                            return;
                        }
                    }
                    else
                    {
                        dropInfo.Effects = DragDropEffects.None;
                        return;
                    }
                }
                else
                {
                    return;
                }
            }
            catch
            {
                return;
            }

        }

        public bool IsDropTargetChild(IItemProjectTree sourceItem, IItemProjectTree targetItem)
        {
            // Recursively checks if the targetItem is a child of sourceItem to prevent circular references
            foreach (IItemProjectTree child in sourceItem.Children)
            {
                if (targetItem == child)
                {
                    return true; // Found a match, prevent drop
                }

                if (IsDropTargetChild(child, targetItem))
                {
                    return true; // Recursively check children
                }
            }
            return false;
        }

        void IDropTarget.Drop(IDropInfo dropInfo)
        {
            IItemProjectTree sourceItem = dropInfo.Data as IItemProjectTree;
            IItemProjectTree targetItem = dropInfo.TargetItem as IItemProjectTree;

            if (sourceItem == null || targetItem == null) return;

            var copy = sourceItem.Copy();
            targetItem.Children.Add(copy);

            Xml.UpdateLayout();

        }

        private bool IsAllowedChild(IItemProjectTree targetItem, IItemProjectTree sourceItem)
        {
            return true;
        }


    }
}
